const api = require("../../http/api");
const API = require("../../http/api")
const AUTH = require("../../utils/auth")
// pages/cate/cate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:[],
    listId:0,
    active:0,
    page:1,
    pageSize:6,
    listSp:[],
    iss:true,
    di:false,
    fenid:'',
    show:false,
    num:0
  },
  onClose() {
    this.setData({ show: false,  properties:null,});
    wx.showTabBar()
  },

 async addgou(e){
     console.log(e.currentTarget.dataset.id)
     const content=this.data.listSp.find(item=>{
       return item.id==e.currentTarget.dataset.id
     })
    
      if(content.stores<=0){
        wx.showToast({
          title: '已卖空',
          icon:'none'
        })
        return
      }

      this.issdeng({
        goodsId:content.id,
        number:1,
        sku:[]
      })
  },
  async issdeng(arr){
    AUTH.checkHasedLogined().then(isLogined => {
      console.log(isLogined)
      if (isLogined) {
        // 处理加入购物车的业务逻辑
        this.addGWC(arr)
      } else {
        AUTH.login(this)
      }
    })
  },
  async addGWC(arr){
    console.log(arr)
      const res=await API.getaddgwc({token:wx.getStorageSync('token'),sku:arr.sku,number:arr.number,goodsId:arr.goodsId})
      console.log(res)
      if(res.code==30002){
        const res=await API.getxq(arr.goodsId)
        console.log(res)
          wx.hideTabBar()
        this.setData({ show: true });
        const goods=res.data
        goods.basicInfo.num=1
      this.setData({
        goods,
        selectSizePrice: goods.basicInfo.minPrice,
        selectSizeOPrice: goods.basicInfo.originalPrice,
       properties:res.data.properties,
       basicInfo:res.data.basicInfo
     })
        return
      }
      if(res.code===0){
        wx.showToast({
          title: "加入成功",
          icon: 'none'
        })
        return
      }
      wx.showToast({
        title: '加入成功',
        icon: 'success'
      })
      this.setData({
        properties:null,
        show:false
      })
      wx.showTabBar()

  },
  xuangg(e){
    console.log(e)
    const pid = e.target.dataset.pid
    const id = e.target.dataset.id
    console.log(this.data.goods)
    const cont=this.data.goods.properties.find(item=>{
      return item.id==pid
    })
    console.log('cont::',cont)
    cont.childsCurGoods.forEach(item=>{
       if(item.id==id) {
        //  console.log("item.id",item.id)
        //  console.log("id",id)
        item.active = true
        // console.log(  cont.childsCurGoods)
       }else {
        item.active = false
      }
      this.setData({
        properties: this.data.goods.properties
      })
    })
    this.getjiage()
  },
  async getjiage(){
    let price = this.data.goods.basicInfo.minPrice
    let originalPrice = this.data.goods.basicInfo.originalPrice
    let minScore = this.data.goods.basicInfo.minScore
    let  stores= this.data.goods.basicInfo.stores
    let number = this.data.goods.basicInfo.minBuyNumber

      const guigenum=this.data.goods.properties.length
      let xuannum=0
      let propertyChildIds=""
      // let childname=""
     this.data.goods.properties.forEach(item=>{
       item.childsCurGoods.forEach(it=>{
          if(it.active){
            xuannum++
            propertyChildIds=propertyChildIds+item.id+":"+it.id+','
          }
          // console.log('9999',propertyChildIds)
       })
     })
     let jiage=false
     if(xuannum==guigenum){
      jiage=true
     }
     const token=wx.getStorageSync('token')
     const res=await API.getgoodjiage({
         token:token?'token':'',
         goodsId:this.data.goods.basicInfo.id,
         propertyChildIds:propertyChildIds
     })
     console.log('jiage',res)
     if(res.code==0){
        price = res.data.price
        originalPrice = res.data.originalPrice
        minScore = res.data.score
        number = res.data.stores
     }
     this.setData({
      selectSizePrice: price,
      selectSizeOPrice: originalPrice,
      minScore: minScore,
      stores,
      number: (stores >= number) ? number : 0
    });
    console.log('500',price)
    console.log('562',this.data.selectSizePrice)
  },
  jian(){
    const goods = this.data.goods
    if (goods.basicInfo.num >1) {
      goods.basicInfo.num--
      this.setData({
        goods
      })
    }
  },
  jia(){
    const goods = this.data.goods
   c
    // console.log(this.data.goods.basicInfo)
  },

  // 加入购物车
  addbtngou(){
     const goods=this.data.goods
     const guigelen=goods.properties.length
     const sku=[]
     goods.properties.forEach(item=>{
       const iss=item.childsCurGoods.find(it=>{
         return it.active
       })
       console.log(iss)
       if(!iss){
         return
       }
       console.log(item.childsCurGoods)
       sku.push({
        optionId: iss.propertyId,
        optionValueId: iss.id
       })
       console.log(sku)
     })
     if(sku.length!==guigelen){
       wx.showToast({
         title: '请选择规格',
         icon:'none'
       })
       return
     }
     const arr = {
      goodsId: goods.basicInfo.id,
      number: goods.basicInfo.num,
      sku:JSON.stringify(sku)
    }
    this.addGWC(arr)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    // this.setData({
    //   active:0,
    //   listId:283703
    // })
    this.setData({
      page:1
    })
    this.getfirst()
    this.creatList()
    wx.removeStorageSync('fenid')
    wx.removeStorageSync('fenindex')
    // this.cs()
  },
  goxq(e){
    console.log(e)
    const id=e.currentTarget.dataset.id
     wx.navigateTo({
       url: '/pages/xq/xq?id='+id,
     })
  },

  getfirst(){
    this.setData({
      listSp:[]
    })
   
   const id=wx.getStorageSync('fenid')||""
   const index=wx.getStorageSync('fenindex')||""

  if(index!==""){
    this.data.active=index
  }else{
    this.data.active=0
  }
  this.setData({
    active: this.data.active
  })
  
    if(id!==""){
       this.data.listId=id
    }else{
      this.data.listId=283703
      // this.data.active=0
    }
      this.setData({
        listId:this.data.listId
    })
    console.log("250",this.data.listId)
    this.cs()
    // wx.removeStorageSync('fenid')
  },
  async cs(){
   
   console.log(this.data.listId)
    this.data.iss=false
    const res = await API.getlist({categoryId:this.data.listId,page:this.data.page,pageSize:this.data.pageSize})
    console.log(res)
    if(res.code===0){
      this.data.iss=true
      this.data.listSp=this.data.listSp.concat(res.data.result)
      this.setData({
        listSp:this.data.listSp
      })
      // console.log("0000",this.data.listSp)
    }else{
      this.setData({
        di:true,
       
      })
    }
  },
  async onClickNav(e){
    console.log(e)
   
    this.data.listId = e.currentTarget.dataset.id
    this.data.active = e.currentTarget.dataset.index
    console.log(this.data.listId)
    const res = await API.getlist({categoryId:this.data.listId})
    // console.log(res)
    if(res.code===0){
      // this.data.listSp=this.data.listSp.concat(res.data.result)
      this.setData({
        listSp:res.data.result
      })
      // console.log(res.data.result)
    }else{
      this.setData({
        listSp:[]
      })
    }
    
  },
  //获取上坪列表
  //获取左侧菜单栏的左侧列表
  async creatList(){
    const res = await API.getfen()
    this.data.items=res.data
    this.setData({
      items:this.data.items
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  loadData(){
  
    if(this.data.iss){
      this.data.page++
      // console.log("00000")
      this.cs()
    }
  },
  onReachBottom: function () {
   
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})